<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>watch侦听器</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div id="box">
        <input type="text" v-model="mytext">
        <ul>
            <li v-for="item in datalist" :key="item">
                {{item}}
            </li>
        </ul>
    </div>
</body>
<script>
    let obj = {
        data() {
            return {
                mytext: '',
                datalist: ['aaa', 'bbb', 'ccc','ddd','abc','cdb'],
                baklist: ['aaa', 'bbb', 'ccc','ddd','abc','cdb'],
            }
        },
        watch: {
            mytext(value, oldvalue) {  //注意监听名字要与被监听的状态名一样
                console.log(value, oldvalue);   //第一个值为当前值,第二个值为上一次的值
                this.datalist = this.baklist.filter(item=>item.includes(this.mytext));
            }
        }
    }
    let app = Vue.createApp(obj).mount('#box');
</script>

</html>